function ajax(option) {
    return new Promise(resolve => {
        let o2s = obj => Object.keys(obj).map(key => key + "=" + obj[key]).join("&");
        let xhr;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }
        else {
            xhr = new ActiveXObject();
        }

        // 
        let { url = "", method = "get", async = true, data = {}, contentType = true, success = function () { }, dataType = "text" } = option;

        if (method.toLowerCase() == "get") {
            url = url + "?" + o2s(data);
        }

        xhr.open(method, url, async);
        if (method.toLowerCase() == "post") {
            if (contentType) {
                // 
                xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded")
            }
        }

        xhr.onload = function () {
            if (dataType.toLowerCase() == "text") {
                success(xhr.responseText);
                resolve(xhr.responseText);
            }
            else if (dataType.toLowerCase() == "json") {
                success(JSON.parse(xhr.responseText));
                resolve(JSON.parse(xhr.responseText));
            }
        }
        if (method.toLowerCase() == "post") {
            if (contentType) {
                let sendData = o2s(data);
                xhr.send(sendData);
            } else {
                xhr.send(data);
            }
        }
        else {
            xhr.send();
        }
    }
    );
}

